package com.mm.serivce.taxi.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mm.domain.taxi.DTaxiAddressInfo;
import com.mm.mapper.taxi.DTaxiAddressInfoMapper;
import com.mm.serivce.taxi.IDTaxiAddressInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户打车地址表 服务实现类
 * </p>
 *
 * @author JueDream
 * @since 2021-08-04
 */
@Service
public class DTaxiAddressInfoServiceImpl extends ServiceImpl<DTaxiAddressInfoMapper, DTaxiAddressInfo> implements IDTaxiAddressInfoService {

    private final DTaxiAddressInfoMapper addressInfoMapper;

    public DTaxiAddressInfoServiceImpl(DTaxiAddressInfoMapper addressInfoMapper) {
        this.addressInfoMapper = addressInfoMapper;
    }

    @Override
    public Page<DTaxiAddressInfo> queryAddressPage(Integer page, Integer limit, String addressForm, String addressLast) {
        Page<DTaxiAddressInfo> addressInfoPage = new Page<>(page, limit);
        LambdaQueryWrapper<DTaxiAddressInfo> queryWrapper = new LambdaQueryWrapper<DTaxiAddressInfo>();
        if (StrUtil.isNotBlank(addressForm)) {
            queryWrapper.like(DTaxiAddressInfo::getTaxiFirst, addressForm);
        }
        if (StrUtil.isNotBlank(addressLast)) {
            queryWrapper.like(DTaxiAddressInfo::getTaxiLast, addressLast);
        }
        queryWrapper.eq(DTaxiAddressInfo::getDelFlg, '0');
        addressInfoPage = addressInfoMapper.selectPage(addressInfoPage, queryWrapper);
        addressInfoPage.setTotal(addressInfoPage.getRecords().size());
        return addressInfoPage;
    }

    @Override
    public List<DTaxiAddressInfo> queryTaxiAddressAll() {
        LambdaQueryWrapper<DTaxiAddressInfo> queryWrapper = new LambdaQueryWrapper<DTaxiAddressInfo>();
        queryWrapper.eq(DTaxiAddressInfo::getDelFlg, '0');
        List<DTaxiAddressInfo> taxiAddressInfos = addressInfoMapper.selectList(queryWrapper);
        return taxiAddressInfos;
    }

    @Override
    public DTaxiAddressInfo queryAddressOne(Integer taxiId) {
        DTaxiAddressInfo addressInfo = new DTaxiAddressInfo();
        if (taxiId > 0) {
            addressInfo = addressInfoMapper.selectById(taxiId);
        }
        return addressInfo;
    }

    @Override
    public boolean removeAddress(Integer taxiId) {
        boolean flag = false;
        DTaxiAddressInfo addressInfo = addressInfoMapper.selectById(taxiId);
        if (Objects.nonNull(addressInfo)) {
            addressInfo.setDelFlg(1);
            Integer result = addressInfoMapper.updateById(addressInfo);
            if (result > 0) {
                flag = true;
            }
        }
        return flag;
    }
}
